package com.sinog.biz.schedule;

import com.sinog.biz.service.business.SentenceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
 * 安置系统-redis清理更新罪名信息
 * @author lk
 */
@Slf4j
@Component
@Configuration
@ConditionalOnProperty(name = "gkzx.azbj.zuiming.open", havingValue = "true")
public class ChargeSchedule {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SentenceService sentenceService;

    @Scheduled(cron = "${gkzx.azbj.zuiming.cron}")
    @Transactional(rollbackFor = Exception.class)
    public void executeDepartChild() {
        log.info("罪名缓存表开始整理");
        String deleteSql = "delete from critype_child where id in (2,15,58,161,199,212,341,363,376,408,9999)";
        jdbcTemplate.update(deleteSql);
        String insertSql1 = "INSERT INTO critype_child (id, childid) VALUES (2, '$,2,3,4,5,6,7,8,9,10,11,12,13,14,3001,4001,6001,6002,6003,6004,7001,7002,7003,7004,7005,7006,7007,7008,7009,7010,7011,8001,10001,12001,12002,12003,12004,12005,12006,12007,12008,12009,12010,12011,12012,12013,12014,12015,12016,12017,12018,12019,12020,12021,12022,12023,12024,12025,12026,12027,12028,12029,12030,12031,12032,12033,12034,12035,12036,12037,12038,12039,12040,12041,12042,12043')";
        String insertSql2 = "INSERT INTO critype_child (id, childid) VALUES (15, '$,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,4120,4121,4122,4123,4124,4125,4163,4173,4174,4175,4176,4177,4201,16001,17001,19001,19002,19003,20001,20002,20003,20004,20005,26001,27001,28001,29001,29002,31001,32001,33001,33002,33003,33004,33005,33006,33007,34001,34002,34003,34004,35001,36001,36002,36003,36004,37001,38001,38002,38003,38004,38005,38006,39001,39002,39003,39004,39005,40001,40002,40003,40004,40005,40006,40007,40008,40009,40010,40011,40012,40013,40014,40015,40016,40017,40018,40019,40020,40021,40022,40023,40024,40025,40026,40027,40028,40029,40030,40031,40032,40033,40034,40035,40036,40037,40038,40039,40040,40041,40042,40043,40044,40045,40046,40047,40048,40049,40050,40051,40052,40053,40054,40055,40056,40057,40058,40059,40060,40061,40062,40063,40064,40065,40066,40067,40068,40069,40070,40071,40072,40073,40074,40075,40076,40077,40078,40079,40080,40081,40082,40083,40084,40085,41001,41002,42001,42002,42003,42004,42005,43001,43002,43003,43004,43005,43006,43007,43008,43009,43010,44001,44002,45001,45002,45003,45004,45005,45006,45007,45008,45009,46001,46002,47001,48001,48002,48003,48004,48005,48006,48007,48008,48009,48010,48011,48012,48013,48014,48015,48016,48017,51001,52001,54001,55001,4123001,4123002,4123003,4123004,4123005,4123006,4123007,4123008,4123009,4123010,4123011,4124001,4124002,4124003,4124004,4124005,4124006,4124007,4124008,4125001,4125002,4125003,4125004,4125005,4125006,4125007,4176001')";
        String insertSql3 = "INSERT INTO critype_child (id, childid) VALUES (58, '$,58,59,69,80,93,118,127,140,148,60,61,62,63,64,65,66,67,68,70,71,72,73,74,75,76,77,78,79,81,82,83,84,85,86,87,88,89,90,91,92,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,119,120,121,122,123,124,125,126,128,129,130,131,132,133,134,135,136,137,138,139,141,142,143,144,145,146,147,149,150,151,152,153,154,155,156,157,158,159,160,4086,4092,4093,4119,4128,4129,4135,4143,4144,4145,4146,4147,4148,4149,4154,4155,4164,4166,4167,59001,59002,4207,60001,60002,60003,60004,60005,60006,61001,61002,61003,61004,61005,62001,62002,63001,63002,64001,64002,64003,64004,64005,64006,64007,64008,64009,64010,64011,64012,64013,64014,64015,64016,64017,64018,65001,65002,65003,65004,65005,66001,66002,66003,66004,66005,66006,66007,66008,66009,66010,66011,66012,66013,66014,66015,66016,66017,66018,66019,66020,66021,66022,66023,66024,66025,66026,66027,66028,66029,66030,66031,66032,66033,66034,66035,66036,68001,68002,70001,70002,73001,74001,74002,75001,75002,76001,76002,76003,76004,76005,76006,76007,76008,76009,76010,78001,78002,78003,78004,79001,81001,82001,82002,83001,83002,84001,84002,84003,85001,85002,85003,85004,85005,85006,86001,86002,86003,86004,87001,87002,87003,89001,89002,89003,91001,91002,91003,91004,91005,91006,91007,94001,94002,95001,95002,95003,95004,95005,95006,95007,95008,95009,95010,95011,95012,95013,95014,95015,95016,95017,95018,96001,97001,97002,97003,97004,97005,97006,97007,97008,97009,100001,100002,100003,100004,100005,100006,100007,100008,100009,100010,103001,103002,103003,103004,103005,103006,103007,103008,104001,104002,104003,104004,104005,104006,104007,104008,104009,104010,104011,104012,104013,104014,104015,104016,104017,104018,108001,108002,109001,109002,110001,110002,110003,110004,110005,113001,113002,113003,113004,113005,113006,114001,114002,115001,115002,115003,115004,115005,115006,116001,116002,119001,121001,121002,126001,128001,128002,128003,129001,131001,131002,131003,132001,132002,132003,132004,132005,132006,132007,132008,132009,133001,133002,133003,134001,134002,134003,134004,136001,136002,136003,136004,136005,136006,136007,136008,136009,137001,137002,137003,137004,137005,137006,137007,137008,137009,137010,137011,137012,137013,138001,138002,141001,143001,143002,143003,146001,149001,149002,152001,153001,153002,153003,153004,155001,155002,155003,155004,155005,155006,155007,155008,155009,155010,155011,155012,155013,156001,156002,157001,157002,4086001,4086002,4086003,4086004,4086005,4086006,4092001,4092002,4092003,4092004,4093001,4093002,4093003,4093004,4093005,4093006,4119001,4119002,4135001,4135002,4145001,4145002,4145003,4145004,4146001,4146002,4146003,4146004,4146005,4146006,4148001,4149001,4154001,4155001,4155002,4155003,4155004,4164001,4164002,4164003,4164004,4164005,4166001,4167001')";
        String insertSql4 = "INSERT INTO critype_child (id, childid) VALUES (161, '$,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,4136,4140,4141,4142,4150,4156,4158,4168,4169,4178,162001,162002,162003,162004,163001,163002,164001,164002,164003,164004,165001,165002,166001,166002,166003,166004,166005,166006,167001,167002,168001,168002,168003,168004,168005,168006,168007,168008,169001,170001,170002,171001,171002,171003,171004,171005,172001,172002,172003,173001,173002,173003,173004,174001,174002,174003,174004,174005,178001,183001,184001,184002,184003,185001,185002,186001,188001,188002,188003,188004,189001,189002,189003,189004,189005,189006,189007,189008,189009,189010,189011,189012,189013,189014,189015,189016,189017,189018,189019,189020,189021,189022,189023,191001,191002,194001,194002,195001,196001,196002,4142001,4142002,4150001,4150002,4158001,4158002,4158003,4158004,4158005')";
        String insertSql5 = "INSERT INTO critype_child (id, childid) VALUES (199, '$,199,200,201,202,203,204,205,206,207,208,209,210,211,4170,200001,200002,200003,200004,201001,201002,201003,201004,201005,202001,202002,202003,203001,205001,205002,206001,208001,208002,208003,208004,208005,208006,208007,208008,209001,209002,210001,210002,210003,210004,210005,210006,210007,211001,211002')";
        String insertSql6 = "INSERT INTO critype_child (id, childid) VALUES (212, '$,212,213,249,267,276,287,299,314,327,335,4179,4180,4181,4182,4183,4184,4185,4186,4187,4188,4189,4190,4191,4193,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,268,269,270,271,272,273,274,275,277,278,279,280,281,282,283,284,285,286,288,289,290,291,292,293,294,295,296,297,298,300,301,302,303,304,305,306,307,308,309,310,311,312,313,315,316,317,318,319,321,322,323,324,325,326,328,329,330,331,332,333,334,336,337,338,339,340,4130,4131,4132,4137,4151,4159,4160,4171,4202,4203,4204,287001,287002,287003,287004,287005,4194,4195,4196,4197,4198,4199,4205,314001,314002,314003,314004,314005,314006,314007,314008,314009,314010,314011,314012,314013,314014,314015,314016,4206,327001,327002,327003,327004,327005,327006,327007,4179001,4179002,4187001,4187002,4188001,4188002,4191001,4191002,214001,216001,217001,217002,217003,217004,217005,217006,217007,217008,217009,217010,217011,217012,217013,217014,217015,217016,217017,217018,217019,217020,217021,217022,217023,217024,217025,217026,217027,217028,217029,217030,217031,217032,217033,217034,217035,217036,217037,217038,217039,217040,217041,217042,217043,217044,217045,217046,217047,217048,217049,217050,217051,217052,217053,217054,217055,217056,218001,218002,218003,218004,218005,218006,218007,218008,218009,218010,218011,218012,218013,218014,218015,218016,218017,218018,218019,218020,218021,218022,218023,218024,218025,218026,218027,218028,218029,218030,218031,218032,218033,218034,218035,218036,218037,218038,218039,218040,218041,218042,218043,219001,219002,219003,219004,219005,219006,219007,219008,219009,219010,219011,219012,219013,219014,219015,219016,219017,220001,220002,220003,220004,220005,220006,221001,221002,222001,222002,222003,222004,222005,222006,222007,222008,222009,222010,222011,222012,222013,222014,222015,222016,222017,222018,222019,222020,222021,222022,222023,222024,223001,223002,223003,223004,223005,223006,223007,223008,223009,223010,223011,223012,223013,223014,223015,223016,223017,223018,223019,223020,225001,225002,226001,226002,228001,229001,230001,230002,230003,231001,232001,232002,232003,233001,234001,234002,234003,234004,234005,234006,236001,236002,237001,238001,238002,238003,238004,238005,238006,239001,239002,239003,239004,239005,239006,239007,239008,239009,239010,239011,239012,239013,239014,239015,239016,239017,239018,239019,239020,239021,239022,239023,239024,239025,239026,239027,239028,239029,239030,239031,239032,239033,239034,239035,239036,239037,239038,239039,239040,239041,239042,239043,239044,239045,239046,239047,239048,240001,240002,240003,240004,240005,240006,241001,241002,242001,242002,242003,242004,242005,242006,242007,242008,242009,242010,242011,242012,242013,242014,242015,242016,242017,242018,242019,246001,246002,246003,246004,246005,246006,246007,251001,251002,251003,251004,251005,251006,251007,251008,251009,251010,251011,251012,251013,251014,251015,251016,251017,251018,251019,251020,251021,251022,253001,253002,256001,256002,256003,256004,256005,256006,256007,256008,256009,256010,256011,256012,256013,256014,256015,256016,256017,256018,256019,256020,256021,256022,258001,258002,258003,258004,258005,258006,258007,258008,258009,259001,259002,259003,259004,259005,260001,260002,260003,260004,260005,260006,262001,266001,268001,268002,270001,270002,272001,272002,272003,273001,273002,274001,274002,274003,274004,277001,278001,280001,280002,282001,282002,282003,282004,282005,282006,282007,284001,284002,285001,285002,286001,286002,292001,292002,292003,292004,292005,292006,292007,292008,292009,292010,292011,292012,297001,297002,298001,298002,298003,304001,304002,304003,304004,304005,304006,304007,304008,304009,304010,304011,304012,304013,304014,304015,304016,304017,304018,304019,304020,304021,304022,304023,304024,304025,304026,304027,304028,304029,304030,304031,304032,304033,304034,304035,304036,304037,304038,304039,304040,304041,304042,304043,304044,304045,304046,304047,304048,304049,304050,304051,305001,305002,305003,305004,305005,310001,310002,310003,310004,310005,310006,313001,313002,313003,313004,313005,313006,313007,313008,315001,315002,315003,315004,315005,315006,315007,315008,315009,315010,315011,315012,315013,318001,318002,318003,318004,318005,318006,318007,318008,318009,318010,318011,318012,318013,318014,318015,318016,318017,318018,318019,318020,318021,318022,318023,318024,318025,318026,319001,319002,319003,319004,319005,322001,322002,322003,322004,322005,322006,322007,322008,322009,322010,322011,322012,322013,322014,322015,322016,322017,322018,322019,322020,322021,323001,323002,323003,323004,323005,323006,323007,323008,323009,323010,323011,323012,324001,324002,325001,326001,326002,326003,328001,328002,329001,330001,330002,331001,331002,331003,331004,331005,331006,331007,331008,331009,331010,331011,336001,336002,336003,336004,336005,336006,336007,336008,336009,336010,336011,336012,336013,336014,336015,336016,336017,336018,336019,336020,336021,336022,336023,336024,336025,336026,336027,336028,338001,4131001,4131002,4137001,4137002,4137003,4137004,4137005,4137006,4137007,4137008,4137009,4137010,4137011,4137012,4137013,4160001,4160002,4160003,4160004,4160005,4160006,4171001,4171002,4171003,4171004,4171005,4171006,4171007,4171008')";
        String insertSql7 = "INSERT INTO critype_child (id, childid) VALUES (341, '$,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,4152,4161,344001,344002,344003,344004,344005,344006,345001,345002,346001,346002,353001,353002,353003,353004,353005,353006,353007,353008,353009,353010,353011,353012,353013,353014,353015,353016,353017,353018,353019,353020,353021,353022,353023,353024,353025,353026,353027,353028,353029,353030,353031,353032,353033,353034,353035,353036,353037,353038,353039,353040,353041,353042,353043,353044,353045,353046,353047,353048,353049,354001,354002,354003,354004,354005,354006,354007,354008,354009,354010,354011,354012,354013,354014,355001,355002,356001,356002,356003,356004,356005,356006,357001,357002,361001,361002,362001,362002,4152001,4152002,4152003,4161001,4161002,4161003,4161004,4161005,4161006,4161007,4161008,4161009,4161010,4161011,4161012,4161013')";
        String insertSql8 = "INSERT INTO critype_child (id, childid) VALUES (363, '$,363,364,365,366,367,368,369,370,371,372,373,374,375,4162,4192,4165,372001,4165001,4165002,4165003')";
        String insertSql9 = "INSERT INTO critype_child (id, childid) VALUES (376, '$,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,809,810,4133,4134,4138,4139,4153,4172,377001,379001,383001,385001,385002,385003,385004,385005,385006,385007,385008,385009,385010,385011,385012,386001,387001,387002,387003,387004,388001,388002,388003,389001,389002,389003,389004,389005,389006,389007,390001,390002,393001,394001,394002,394003,398001,399001,401001,401002,401003,401004,401005,402001,402002,403001,403002,403003,403004,403005,403006,403007,403008,404001,404002,404003,404004,404005,404006,404007,404008,404009,404010,404011,404012,404013,404014,404015,404016,404017,404018,406001,406002,406003,406004,406005,407001,407002,4133001,4133002,4134001,4134002,4138001,4138002,4139001,4139002')";
        String insertSql10 = "INSERT INTO critype_child (id, childid) VALUES (408, '$,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,4208,4209,410001,410002,411001,411002,414001,414002,414003,421001,421002,421003,421004,421005,421006,421007,421008,421009,421010,421011,421012,421013,421014,421015,424001,428001,428002,428003,428004,428005,428006,430001,430002,433001,433002,437001,437002,437003,437004')";
        String insertSql11 = "INSERT INTO critype_child (id, childid) VALUES (9999, '$,9999,9999901,9999902,9999903,9999904,9999906,9999907,9999909,9999910,9999911,9999912,9999913,9999914,9999915,9999916,9999917,9999918,9999919,9999920,9999921,9999922,9999923,9999924,9999925')";
        jdbcTemplate.batchUpdate(insertSql1, insertSql2, insertSql3, insertSql4, insertSql5, insertSql6, insertSql7, insertSql8, insertSql9, insertSql10, insertSql11);
        log.info("罪名缓存表结束整理");
        log.info("---------------------------------------------------");
        log.info("初始化redis罪名缓存表开始");
        RedisSerializer<String> serializer = new StringRedisSerializer();
        stringRedisTemplate.setKeySerializer(serializer);
        stringRedisTemplate.setValueSerializer(serializer);
        stringRedisTemplate.setHashKeySerializer(serializer);
        stringRedisTemplate.setHashValueSerializer(serializer);
        BoundZSetOperations<String, String> zuiming = stringRedisTemplate.opsForZSet().getOperations().boundZSetOps("zuiming");
        long count = stringRedisTemplate.opsForZSet().zCard("zuiming");
        zuiming.removeRange(0,count);
        List<Map<String, String>> allZuiMingList = sentenceService.getAllZuiMingList();
        log.info("初始化redis罪名缓存表结束:{}：条",allZuiMingList.size());
    }
}